package com.franklin.ideaplugin.maphelper.resolver.impl.collection.json.fastjson;

import cn.hutool.core.collection.CollectionUtil;
import com.franklin.ideaplugin.maphelper.beans.colmember.IColMember;
import com.franklin.ideaplugin.maphelper.col.declaration.IColDeclaration;
import com.franklin.ideaplugin.maphelper.resolver.impl.collection.CollectionMethodCallExpressionColMemberResolver;
import com.franklin.ideaplugin.maphelper.resolver.impl.collection.CollectionNewExpressionColMemberResolver;
import com.franklin.ideaplugin.maphelper.utils.FastJsonUtils;
import com.google.common.collect.Sets;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethodCallExpression;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author Ye Junhui
 * @since 2024/3/21
 */
public class FastJsonParseArrayMethodCallExpressionColMemberResolver
        extends CollectionMethodCallExpressionColMemberResolver {

    public final static FastJsonParseArrayMethodCallExpressionColMemberResolver INSTANCE = new FastJsonParseArrayMethodCallExpressionColMemberResolver();

    @Override
    public boolean supports(IColDeclaration colDeclaration) {
        PsiMethodCallExpression psiMethodCallExpression = colDeclaration.getDeclaration().getCreateExpression(PsiMethodCallExpression.class);
        return FastJsonUtils.isJsonParseMethod(
                psiMethodCallExpression,
                "parseArray"
        );
    }

    @Override
    public Collection<IColMember> resolveColMembers(PsiElement psiExpression, IColDeclaration colDeclaration) {
        //结果
        Set<IColMember> result = Sets.newLinkedHashSet();

        //当前代码块
        Collection<IColMember> currentMembers = CollectionNewExpressionColMemberResolver.INSTANCE.resolveColMembers(psiExpression, colDeclaration);
        CollectionUtil.addAll(result,currentMembers);

        PsiMethodCallExpression psiMethodCallExpression = colDeclaration.getDeclaration().getCreateExpression(PsiMethodCallExpression.class);

        //字符串成员
        List<IColMember> stringMembers = FastJsonUtils.findJsonObjectStringMembers(
                psiMethodCallExpression,
                "parseArray"
        );
        CollectionUtil.addAll(result,stringMembers);

        return result;
    }
}
